/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_ttc7_tti_iov.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/30 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frlog.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_ttc7_tti_rxh.h"
#include "ora_ttc7_tti_iov.h"
#include "../../comm/ora_stmt.h"


/*******************************************************************************
*  FUNC     :  对应于 9i的 oracle.jdbc.ttc7.TTIiov的 public void unmarshal() 方法
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc7_tti_iov_unmarshal(Conntrack *p_st_conn
                            , L7session_ora *p_st_sess
                            , L7_ora_packet *p_st_ora_packet)
{
    Sql_statement *p_st_stmt = NULL;
    Sql_metadata_info *p_st_metadata = NULL;
    Frlist_node *p_st_node = NULL;
    short s_tmp = 0;

    if ((p_st_stmt = ora_get_current_stmt(p_st_conn)) == NULL)
        goto error;

    if (ora_ttc7_tti_rxh_unmarshal(p_st_conn, p_st_sess, p_st_ora_packet, p_st_stmt) != FR_SUCC)
        goto error;

    p_st_node = FRLIST_FIRST_NODE(&(p_st_stmt->st_metadata_bind));
    while (p_st_node)
    {
        p_st_metadata = (Sql_metadata_info *) FRLIST_GET_NODE_DATA(p_st_node);

        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // bindtype
            goto error;
        if (s_tmp == 0)
        {
            ORA_PRINT_EMERG("bindtype == 0\n");
            ORA_FRLOG_ERROR("bindtype == 0");
            goto error;
        }
        else if ((s_tmp & 0x10) == 0)
        {
            ORA_PRINT_DBG("bindtype = %02x, i_isuseless = 1\n", s_tmp);
            p_st_metadata->i_isuseless = FR_YES;
        }

        p_st_node = FRLIST_NEXT_NODE(p_st_node);
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
